package com.zhentao.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.dto.ParamValueDTO;
import com.zhentao.dto.ParamValueQueryDTO;
import com.zhentao.service.ParamValueService;
import com.zhentao.util.Result;
import com.zhentao.vo.ParamValueVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "参数值", description = "参数值管理接口")
@RestController
@RequestMapping("/param/value")
@RequiredArgsConstructor
@Slf4j
public class ParamValueController {

    private final ParamValueService paramValueService;

    @Operation(summary = "分页查询参数值")
    @GetMapping("/page")
    public Result page(ParamValueQueryDTO query) {
        Page<ParamValueVO> data = paramValueService.queryParamValues(query);
        return Result.OK(data);
    }

    @Operation(summary = "参数值详情")
    @GetMapping("/{id}")
    public Result detail(@PathVariable Long id) {
        return Result.OK(paramValueService.getParamValueDetail(id));
    }

    @Operation(summary = "创建参数值")
    @PostMapping
    public Result create(@Valid @RequestBody ParamValueDTO dto) {
        boolean ok = paramValueService.createParamValue(dto);
        return ok ? Result.OK(true) : Result.ERROR("创建失败");
    }

    @Operation(summary = "更新参数值")
    @PutMapping("/{id}")
    public Result update(@PathVariable Long id, @Valid @RequestBody ParamValueDTO dto) {
        boolean ok = paramValueService.updateParamValue(id, dto);
        return ok ? Result.OK(true) : Result.ERROR("更新失败");
    }

    @Operation(summary = "删除参数值")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        boolean ok = paramValueService.deleteParamValue(id);
        return ok ? Result.OK(true) : Result.ERROR("删除失败");
    }

    @Operation(summary = "历史记录")
    @GetMapping("/history/{paramId}")
    public Result history(@PathVariable Long paramId) {
        List<ParamValueVO> list = paramValueService.getParamValueHistory(paramId);
        return Result.OK(list);
    }

    @Operation(summary = "启停参数值")
    @PutMapping("/{id}/status/{isActive}")
    public Result toggle(@PathVariable Long id, @PathVariable Integer isActive) {
        boolean ok = paramValueService.toggleParamValueStatus(id, isActive);
        return ok ? Result.OK(true) : Result.ERROR("操作失败");
    }
}


